package com.ruoyi.gen.util;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

/**
 * 项目路径工具类
 * 
 * @author ruoyi
 */
public class ProjectPathUtils {

    /**
     * 获取项目路径信息
     * 
     * @return 项目路径信息
     */
    public static Map<String, String> getProjectPaths() {
        Map<String, String> paths = new HashMap<>();
        
        String currentDir = System.getProperty("user.dir");
        File currentFile = new File(currentDir);
        
        // 设置后端默认路径
        paths.put("backendPath", currentDir);
        
        // 查找前端项目路径
        String frontendPath = findFrontendPath(currentFile);
        paths.put("frontendPath", frontendPath);
        
        // 设置项目根路径
        File parentDir = currentFile.getParentFile();
        if (parentDir != null) {
            paths.put("projectRoot", parentDir.getAbsolutePath());
        } else {
            paths.put("projectRoot", currentDir);
        }
        
        return paths;
    }
    
    /**
     * 查找前端项目路径
     * 
     * @param currentFile 当前项目文件
     * @return 前端项目路径
     */
    private static String findFrontendPath(File currentFile) {
        // 首先检查上级目录中的 RuoYi-Vue3
        File parentDir = currentFile.getParentFile();
        if (parentDir != null) {
            File vue3Dir = new File(parentDir, "RuoYi-Vue3");
            if (vue3Dir.exists() && vue3Dir.isDirectory()) {
                File srcDir = new File(vue3Dir, "src");
                if (srcDir.exists() && srcDir.isDirectory()) {
                    return srcDir.getAbsolutePath();
                }
                return vue3Dir.getAbsolutePath() + File.separator + "src";
            }
            
            // 检查其他可能的前端目录名
            String[] frontendDirNames = {"frontend", "ui", "web", "vue", "ruoyi-ui"};
            for (String dirName : frontendDirNames) {
                File frontendDir = new File(parentDir, dirName);
                if (frontendDir.exists() && frontendDir.isDirectory()) {
                    File srcDir = new File(frontendDir, "src");
                    if (srcDir.exists() && srcDir.isDirectory()) {
                        return srcDir.getAbsolutePath();
                    }
                    return frontendDir.getAbsolutePath() + File.separator + "src";
                }
            }
        }
        
        // 检查当前目录下的前端目录
        String[] localFrontendDirs = {"frontend", "ui", "web", "vue", "src/main/webapp"};
        for (String dirName : localFrontendDirs) {
            File frontendDir = new File(currentFile, dirName);
            if (frontendDir.exists() && frontendDir.isDirectory()) {
                return frontendDir.getAbsolutePath();
            }
        }
        
        // 默认返回当前目录下的 frontend/src
        return currentFile.getAbsolutePath() + File.separator + "frontend" + File.separator + "src";
    }
    
    /**
     * 获取相对路径
     * 
     * @param basePath 基础路径
     * @param targetPath 目标路径
     * @return 相对路径
     */
    public static String getRelativePath(String basePath, String targetPath) {
        if (StringUtils.isEmpty(basePath) || StringUtils.isEmpty(targetPath)) {
            return targetPath;
        }
        
        try {
            File baseFile = new File(basePath);
            File targetFile = new File(targetPath);
            
            String basePath2 = baseFile.getCanonicalPath();
            String targetPath2 = targetFile.getCanonicalPath();
            
            if (targetPath2.startsWith(basePath2)) {
                String relativePath = targetPath2.substring(basePath2.length());
                if (relativePath.startsWith(File.separator)) {
                    relativePath = relativePath.substring(1);
                }
                return relativePath;
            }
        } catch (Exception e) {
            // 忽略异常，返回原路径
        }
        
        return targetPath;
    }
    
    /**
     * 标准化路径分隔符
     * 
     * @param path 路径
     * @return 标准化后的路径
     */
    public static String normalizePath(String path) {
        if (StringUtils.isEmpty(path)) {
            return path;
        }
        return path.replace("\\", File.separator).replace("/", File.separator);
    }
}
